﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HAMath
{


    public class ComputeNew2DGridPoint
    {
        /// <summary>
        ///取得二维平面网格旋转平移后新的点坐标  X Y的旋转平移  逆时针旋转的角度为正值
        /// 操作的顺序是：先旋转XY 后平移XY
        /// </summary>
        /// <param name="x">观测点的X</param>
        /// <param name="y">观测点的Y</param>
        /// <param name="orgX">X方向的平移</param>
        /// <param name="orgY">Y方向的平移</param>
        /// <param name="rotate">旋转角度  采用角度制</param>
        /// <returns></returns>
        public static Point2D GetNewPoint2D(double x, double y,double orgX,double orgY, double rotate)
        {
            //分析说明：假设点A坐标（X,Y）,点A与坐标原点形成的向量OA，
            //与X轴正向夹角R(region),将OA旋转 rotate，形成的新的点B的位置为（nX，nY）




            // nX =  System.Math.Sin((R + rotate)/180.0 * System.Math .PI ) * lengthOA;
            //nY = System.Math.Cos((R + rotate) / 180.0 * System.Math.PI) * lengthOA; 


            // Math.Sin((R + rotate) / 180.0 * Math.PI) = Math.Sin(R / 180.0 * Math.PI) * Math.Cos(rotate / 180.0 * Math.PI) + Math.Cos(R / 180.0 * Math.PI) * Math.Sin(rotate /180.0*Math.PI );
            //Math.Cos ((R+rotate )/180.0*Math.PI )=Math.Cos (R/180.0*Math.PI )*Math.Cos (rotate /180.0*Math.PI )-Math.Sin (R/180.0*Math.PI )*Math.Sin (rotate /180.0*Math.PI )


            //Math.Sin(R / 180.0 * Math.PI) = y / Math.Sqrt(x * x + y * y);
            //Math.Cos(R / 180.0 * Math.PI) = x / Math.Sqrt(x * x + y * y);



            try
            {


                double lengthOA = Math.Sqrt(x * x + y * y);

                Point2D point2d = new Point2D();

                if (lengthOA == 0)
                {
                   
                    point2d.X =  orgX;
                    point2d.Y =  orgY;

                    return point2d;
                }


                double nX2;
                double nY2;

                //绕原点旋转
                nX2 = x * Math.Cos(Math.PI * rotate / 180.0) - y * Math.Sin(Math.PI * rotate / 180.0);
                nY2 = x * Math.Sin(Math.PI * rotate / 180.0) + y * Math.Cos(Math.PI * rotate / 180.0);

                double nXResult;
                double nYResult;
                //平移
                nXResult = nX2 + orgX;
                nYResult = nY2 + orgY;

                #region 未使用

                //double nX;
                //double nY;

                //double sinR = y / Math.Sqrt(x * x + y * y);
                //double cosR = x / Math.Sqrt(x * x + y * y);


                //double sinRrotate = sinR * Math.Cos(rotate / 180.0 * Math.PI) + cosR * Math.Sin(rotate / 180.0 * Math.PI);
                //double cosRrotate = cosR * Math.Cos(rotate / 180.0 * Math.PI) - sinR * Math.Sin(rotate / 180.0 * Math.PI);


                //nX = cosRrotate * lengthOA;
                //nY = sinRrotate * lengthOA;



                ////Point2D point2d = new Point2D();
                //point2d.X = nX+orgX ;
                //point2d.Y = nY+orgY ;

                #endregion

                point2d.X = nXResult;
                point2d.Y = nYResult;



                return point2d;


            }
            catch
            {

                return null;
            }

        }





    }
}
